package com.atguigu.tingshu.user.api;

import com.atguigu.tingshu.common.login.CustomLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.user.service.WxLoginService;
import com.atguigu.tingshu.user.strategy.cilent.LoginClient;
import com.atguigu.tingshu.user.strategy.domain.CommonLoginParam;
import com.atguigu.tingshu.user.strategy.enums.LoginType;
import com.atguigu.tingshu.vo.user.UserInfoVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Tag(name = "微信授权登录接口")
@RestController
@RequestMapping("/api/user/wxLogin")
@Slf4j
public class WxLoginApiController {

    @Autowired
    private WxLoginService wxLoginService;

    @Autowired
    private LoginClient loginClient;

    @Operation(summary = "小程序授权登录")
    @GetMapping("/wxLogin/{code}")
    public Result wxLogin(@PathVariable("code") String code) {
        CommonLoginParam loginParam = CommonLoginParam.builder().code(code).build();
        Map<String, String> resultMap = loginClient.login(LoginType.WX_LOGIN, loginParam);
        return Result.ok(resultMap);
    }

    @CustomLogin
    @Operation(summary = "获取用户登录信息")
    @GetMapping("/getUserInfo")
    public Result getUserInfo() {
        UserInfoVo vo = wxLoginService.getUserInfo();
        return Result.ok(vo);
    }

    @CustomLogin
    @Operation(summary = "更新用户信息")
    @PostMapping("updateUser")
    public Result updateUser(@RequestBody UserInfoVo userInfoVo){
        wxLoginService.updateUser(userInfoVo);
        return Result.ok();
    }
}
